﻿using linghaoDal;
using linghaoModel.ApiRequest;
using linghaoModel.Entitys;

namespace linghaoBll
{
    public class WebTypeBll
    {
        public DbFunction<WebType> dbFunction = new DbFunction<WebType>();
        public async Task<string> Insert(WebTypeRequest insertWebRequest)
        {
            #region 校验
            if (insertWebRequest == null)
            {
                return "请输入数据";
            }
           
            if (string.IsNullOrEmpty(insertWebRequest.Type))
            {
                return "请输入网站分类名称";
            }
           
            #endregion

            var webTable = new WebType()
            {
                Type = insertWebRequest.Type
            };
            // 这里可以调用数据库操作方法进行插入
            string sql = "INSERT INTO WebType (Type) VALUES (@Type)";
            await dbFunction.ExecuteAsync(sql, webTable);
            return Ok();
        }

        public async Task<IEnumerable<WebType>> Select()
        {
            string sql = "SELECT * FROM WebType";
           
            return await dbFunction.QueryAsync(sql);
        }

        public async Task<string> Delete(int Id)
        {
            if (Id <= 0)
            {
                return "未获取到网站类型信息";
            }
            await dbFunction.ExecuteAsync("DELETE FROM WebType WHERE Id = @Id", new { Id });
            return Ok();
        }

        public async Task<string> Update(WebTypeRequest updateWebRequest)
        {
            #region 校验
            if (updateWebRequest == null)
            {
                return "请输入数据";
            }

            if (string.IsNullOrEmpty(updateWebRequest.Type))
            {
                return "请输入网站分类名称";
            }
            #endregion

            var webTable = new WebType()
            {
                Id = updateWebRequest.Id ?? 0,
                Type = updateWebRequest.Type

            };
            // 这里可以调用数据库操作方法进行插入
            var sql = "UPDATE WebType SET Type = @Type WHERE Id = @Id";
            await dbFunction.ExecuteAsync(sql, webTable);
            return Ok();
        }

        public string Ok()
        {
            return "操作成功";
        }
    }
}
